<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>页码</title>
	<link rel="stylesheet" href="">
</head>
<style type="text/css">
.page{text-align: center;}
	.page a{padding:5px;border:1px solid #ccc;text-decoration: none;padding:2px 5px;margin:2px;}
	.page span.current{border:009 1px solid; background-color: #52A0D0;padding:4px 6px; margin:2xp;color: #fff;font-weight: bold;}
	.page span.disable{border:#eee 1px solid; padding:2px 5px;margin:2px;color:#ddd;}
	.page form{display: inline;}
</style>


<body>
<?php

$page = $_GET['p'];
$host="localhost";
$username="root";
$password="";
$db="test";
$pagesize=4;
$showPage =  5;
$conn=mysql_connect($host,$username,$password);
if(!$conn){
	echo "数据库连接失败！";
	exit;
}

mysql_select_db($db);

mysql_query("SET NAMES UTF8");
$limit =  ($page-1)*$pagesize;
$sql="SELECT * FROM page LIMIT $limit,$pagesize";
$result=mysql_query($sql);
echo "<div class='content'>";
echo '<table border=1 width=40% align=center>';
echo '<tr>';
	echo '<td>ID</td><td>NAME</td>';
echo '</tr>';
while($row=mysql_fetch_assoc($result)){
	echo '<tr>';
	echo "<td>{$row['id']}</td>";
	echo "<td>{$row['name']}</td>";
	echo '</tr>';
}
echo '</table>';
	echo '</div>';
mysql_free_result($result);

$total_sql = "SELECT COUNT(*) FROM page";
$total_result = mysql_fetch_array(mysql_query($total_sql));
$total = $total_result[0];

$total_page=ceil($total/$pagesize);
mysql_close($conn);



$page_banner="<div class='page'>";

$pageoffset = ($showPage-1)/2;


if($page>1){
	$page_banner .="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
	$page_banner .="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'><上一页</a>";
}else{
	$page_banner.="<span class='disable'>首页</span>";
	$page_banner.="<span class='disable'><上一页</span>";
}

$start = 1;
$end =$total_page;
if($total_page>$showPage){
	if($page>$pageoffset+1){
		$page_banner.="...";
	}
	if($page>$pageoffset){
		$start=$page-$pageoffset;
		$end = $total_page>$page+$pageoffset ? $page+$pageoffset:$total_page;
	}else{
		$start = 1;
		$end = $total_page>$showPage?$showPage:$total_page;
	}
	if($page+$pageoffset>$total_page){
		$start=$start-($page+$pageoffset-$end);
	}
}

for($i=$start;$i<=$end;$i++){
	if($page==$i){
		$page_banner .="<span class='current'>$i</span>";
	}else{
	$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".$i."'>{$i}</a>";
	}
}
if($page+$pageoffset<$total_page && $total_page>$showPage){
	$page_banner.="...";
}else{
	$page_banner.="<span class='disable'>尾页</span>";
	$page_banner.="<span class='disable'>下一页></span>";
}

if($page<$total_page){
	$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页></a>";
	$page_banner .="<a href='".$_SERVER['PHP_SELF']."?p=".($total_page)."'>尾页</a>";
}
	$page_banner.="共{$total_page}页";
	$page_banner.="<form action='mypage.php'>";
	$page_banner.="到第<input type='text' size='2' name='p' >页";
	$page_banner.="<input type='submit' value='确定'>";
	$page_banner .="</form>";
	$page_banner .="</div>";
	echo $page_banner;
?>
</body>
</html>
